/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.ap.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.ap.entity.Imprest;
import com.jeeplus.modules.ap.mapper.ImprestMapper;
import com.jeeplus.modules.ap.entity.PaymentWay;
import com.jeeplus.modules.ap.mapper.PaymentWayMapper;

/**
 * 备用金Service
 * @author slD
 * @version 2017-11-29
 */
@Service
@Transactional(readOnly = true)
public class ImprestService extends CrudService<ImprestMapper, Imprest> {

	@Autowired
	private PaymentWayMapper paymentWayMapper;
	
	public Imprest get(String id) {
		Imprest imprest = super.get(id);
		imprest.setPaymentWayList(paymentWayMapper.findList(new PaymentWay(imprest)));
		return imprest;
	}
	
	public List<Imprest> findList(Imprest imprest) {
		return super.findList(imprest);
	}
	
	public Page<Imprest> findPage(Page<Imprest> page, Imprest imprest) {
		return super.findPage(page, imprest);
	}
	
	@Transactional(readOnly = false)
	public void save(Imprest imprest) {
		super.save(imprest);
		for (PaymentWay paymentWay : imprest.getPaymentWayList()){
			if (paymentWay.getId() == null){
				continue;
			}
			if (PaymentWay.DEL_FLAG_NORMAL.equals(paymentWay.getDelFlag())){
				if (StringUtils.isBlank(paymentWay.getId())){
					paymentWay.setImprest(imprest);
					paymentWay.preInsert();
					paymentWayMapper.insert(paymentWay);
				}else{
					paymentWay.preUpdate();
					paymentWayMapper.update(paymentWay);
				}
			}else{
				paymentWayMapper.delete(paymentWay);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Imprest imprest) {
		super.delete(imprest);
		paymentWayMapper.delete(new PaymentWay(imprest));
	}
	
}